<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="utf-8">
    <title>debounce.js - Documentation</title>
    
    
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
    <script src="scripts/nav.js" defer></script>
    
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav >
    
    
    <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#debounce">debounce</a></li><li><a href="global.html#deepCopy">deepCopy</a></li></ul>
    
</nav>

<div id="main">
    
    <h1 class="page-title">debounce.js</h1>
    

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>
/**
 * @description 防抖函数
 * @param {function} func 待执行函数
 * @param {number} delay 延迟时间（毫秒）
 * @param {boolean} immediate 是否立即执行
 * @returns {function} 
 * @example
 // 示例用法
function handleInput() {
  console.log('输入已稳定...');
}
 const debouncedInputHandler = debounce(handleInput, 500);
// 添加事件监听器
document.getElementById('inputElement').addEventListener('input', debouncedInputHandler);
 */
export function debounce(func, delay, immediate = false) {
	let timeoutId;

	return function (...args) {
		const context = this;

		// 清除之前的定时器
		clearTimeout(timeoutId);

		if (immediate &amp;&amp; !timeoutId) {
			// 如果是立即执行模式且没有定时器，立即执行一次函数
			func.apply(context, args);
		}

		// 创建一个新的定时器
		timeoutId = setTimeout(() => {
			if (!immediate) {
				func.apply(context, args);
			}
			timeoutId = null; // 清除定时器标记
		}, delay);
	};
}</code></pre>
        </article>
    </section>




    
    
</div>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Tue Sep 12 2023 13:19:40 GMT+0800 (中国标准时间) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>



</body>
</html>
